//
//  TT_GDCallOutView.m
//  BaseProject
//
//  Created by 王洋 on 2020/4/9.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "TT_GDCallOutView.h"

#define kPortraitMargin     5
#define kPortraitWidth      70
#define kPortraitHeight     50

#define kTitleWidth         120
#define kTitleHeight        20

@interface TT_GDCallOutView ()



@end

@implementation TT_GDCallOutView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
        self.layer.cornerRadius = 8.0f;
        self.userInteractionEnabled = YES;
        self.clipsToBounds = YES;
//        [self pp_clipCorners:UIRectCornerAllCorners radius:6 border:2 color:[UIColor whiteColor]];
        
        [self initSubViews];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
//    [self pp_clipCorners:UIRectCornerAllCorners radius:6 border:2 color:[UIColor whiteColor]];
}

- (void)initSubViews
{
    [self addSubview:self.itemTitleLab];
    [self addSubview:self.itemContentLab];
    [self addSubview:self.ActionBtn];
        
    [self.ActionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(40);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(-10);
    }];
    
    [self.itemTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(self.ActionBtn.mas_left).offset(-10);
        make.height.mas_equalTo(13);
        make.top.mas_equalTo(10);
    }];
    
    [self.itemContentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(self.ActionBtn.mas_left).offset(-10);
        make.top.mas_equalTo(self.itemTitleLab.mas_bottom).offset(5);
        make.bottom.mas_equalTo(-15);
    }];
}

- (void)setTitle:(NSString *)title {
    
    self.itemTitleLab.text = title;
}

- (void)setDesc:(NSString *)desc {
    
    self.itemContentLab.text = desc;
}

- (UIButton *)ActionBtn {
    if (!_ActionBtn) {
        _ActionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_ActionBtn setTitle:@"导航" forState:UIControlStateNormal];
        _ActionBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        [_ActionBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_ActionBtn setBackgroundColor:UIColorHex(0xff4242)];
    }
    return _ActionBtn;;
}

- (UILabel *)itemTitleLab {
    if (!_itemTitleLab) {
        _itemTitleLab = [[UILabel alloc] init];
        _itemTitleLab.font = [UIFont boldSystemFontOfSize:13];
        _itemTitleLab.textColor = UIColorHex(0x333333);
        _itemTitleLab.text = @"title";
    }
    return _itemTitleLab;;
}

- (UILabel *)itemContentLab {
    if (!_itemContentLab) {
        _itemContentLab = [[UILabel alloc] init];
        _itemContentLab.font = [UIFont systemFontOfSize:11];
        _itemContentLab.textColor = UIColorHex(0x999999);
        _itemContentLab.text = @"content";
        _itemContentLab.numberOfLines = 2;
    }
    return _itemContentLab;
}

#define kArrorHeight        10

- (void)drawRect:(CGRect)rect {
    
    [self drawInContext:UIGraphicsGetCurrentContext()];
    self.layer.shadowColor = [[UIColor whiteColor] CGColor];
    self.layer.shadowOpacity = 1.0;
    self.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
}

- (void)drawInContext:(CGContextRef)context {
    
    CGContextSetLineWidth(context, 2.0);
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    [self getDrawPath:context];
    CGContextFillPath(context);
}

- (void)getDrawPath:(CGContextRef)context {
    CGRect rrect = self.bounds;
    CGFloat radius = 6.0;
    CGFloat minx = CGRectGetMinX(rrect),
    midx = CGRectGetMidX(rrect),
    maxx = CGRectGetMaxX(rrect);
    CGFloat miny = CGRectGetMinY(rrect),
    maxy = CGRectGetMaxY(rrect)-kArrorHeight;
    
    CGContextMoveToPoint(context, midx+kArrorHeight, maxy);
    CGContextAddLineToPoint(context,midx, maxy+kArrorHeight);
    CGContextAddLineToPoint(context,midx-kArrorHeight, maxy);
    
    CGContextAddArcToPoint(context, minx, maxy, minx, miny, radius);
    CGContextAddArcToPoint(context, minx, minx, maxx, miny, radius);
    CGContextAddArcToPoint(context, maxx, miny, maxx, maxx, radius);
    CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
    CGContextClosePath(context);
}

//- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
//    [super setSelected:selected animated:animated];
//}
@end
